package net.sureon.app;

import net.sureon.biz.CompanyBO;
import net.sureon.biz.GroupBO;
import net.sureon.biz.UserBO;
import net.sureon.common.security.Role;

/**
 * user session, available for all logged in users.
 * 
 * @author jason
 * 
 */
public class UserSession {
	public static final String HTTP_SESSION_KEY = "userSession";
	private long companyID;
	private long userID;
	private long groupID;
	private String userRealName;
	private Role role;
	private boolean rootLogin;

	public UserSession(CompanyBO company) {
		reset(company);
	}
	public UserSession(GroupBO group) {
		reset(group);
	}	
	public UserSession(UserBO user) {
		reset(user);
	}
	
	public void reset(CompanyBO company) {
		companyID = company.getCompanyID();
	}
	public void reset(GroupBO group) {
		companyID = group.getCompanyID();
		groupID = group.getGroupID();
	}	
	public void reset(UserBO user) {
		companyID = user.getCompanyID();
		userID = user.getUserID();
		groupID = user.getGroupID();
		userRealName = user.getUserVO().getRealName();
		rootLogin = Role.ROLE_NAME_ROOT.equals(user.getUserVO().getRole());
		if(rootLogin){
			role = Role.ROOT;
		}else{
			role = Role.getRole(user.getUserVO().getRole());
		}
	}	
	/**
	 * @return the companyID
	 */
	public long getCompanyID() {
		return companyID;
	}

	/**
	 * @return the userID
	 */
	public long getUserID() {
		return userID;
	}

	/**
	 * @return the groupID
	 */
	public long getGroupID() {
		return groupID;
	}
	
	/**
	 * @return the userRealName
	 */
	public String getUserRealName() {
		return userRealName;
	}

	/**
     * @return the role
     */
    public Role getRole() {
        return role;
    }
    
    public boolean isRootLogin(){
        return rootLogin;
    }
    
    public void setRootLogin(boolean flag){
        rootLogin = flag;
    }
    
    public void reset(){
        companyID = 0;
        groupID = 0;
        userID = 0;
        userRealName = null;
        role = null;
    }

    public String toString() {
		return companyID + ":" + groupID + ":" + userID;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (int) (companyID ^ (companyID >>> 32));
		result = prime * result + (int) (groupID ^ (groupID >>> 32));
		result = prime * result + (int) (userID ^ (userID >>> 32));
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		UserSession other = (UserSession) obj;
		if (companyID != other.companyID)
			return false;
		if (groupID != other.groupID)
			return false;
		if (userID != other.userID)
			return false;
		return true;
	}

	public boolean isValid() {
		return userID > 0;
	}

}
